
<template>
  <div>
    <el-button @click="show_dialog">新增医院</el-button>
    <!-- 文章列表 -->
    <!-- {{ hos.list }} -->
    <el-table :data="hos.list" style="width: 100%">
      <el-table-column prop="name" label="医院名称" align="center">
      </el-table-column>
      <el-table-column prop="introduce" label="医院简介" align="center">
      </el-table-column>
      <el-table-column prop="province" label="医院省份" align="center">
      </el-table-column>
      <el-table-column prop="city" label="医院市级" align="center">
      </el-table-column>
      <el-table-column prop="address" label="医院地址" align="center">
      </el-table-column>
      <el-table-column prop="longitude" label="经度" align="center">
      </el-table-column>
      <el-table-column prop="latitude" label="维度" align="center">
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template v-slot="scope">
          <!-- 修改 -->
          <el-button
            type="warning"
            icon="el-icon-edit"
            circle
            @click="edit_hos(scope.row)"
          ></el-button>
          <!-- 删除 -->
          <el-button
            type="danger"
            icon="el-icon-delete"
            circle
            @click="delete_hos(scope.row.id)"
          ></el-button>
          <!-- <el-link >删除</el-link> -->
          <!-- <el-link >修改</el-link> -->
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页按钮 -->
    <el-pagination
      background
      layout="prev, pager, next"
      :page-size="obj.pageSize"
      :current-page="obj.page"
      @current-change="change"
      :total="hos.total"
    >
    </el-pagination>
    <!-- 模态框 -->
    <el-dialog :title="title" :visible.sync="dialog_hos" width="70%">
      <!-- {{ form_hos }} -->
      <!-- {{ result }} -->
      <el-form :model="form_hos">
        <el-form-item label="医院名称" :label-width="formLabelWidth_hos">
          <el-input v-model="form_hos.name"></el-input>
        </el-form-item>

        <el-form-item label="医院简介" :label-width="formLabelWidth_hos">
          <el-input v-model="form_hos.introduce"></el-input>
        </el-form-item>

        <el-form-item label="省市区" :label-width="formLabelWidth_hos">
          <el-cascader
            v-model="form_hos.pca"
            :options="pca"
            :props="{ expandTrigger: 'hover', label: 'name', value: 'name' }"
            @change="handleChange"
          ></el-cascader>
        </el-form-item>

        <el-form-item label="医院地址" :label-width="formLabelWidth_hos">
          <el-input v-model="form_hos.address"></el-input>
        </el-form-item>

        <el-form-item label="标志" :label-width="formLabelWidth_hos">
          <el-input v-model="form_hos.flags"></el-input>
        </el-form-item>

        <el-form-item label="经度" :label-width="formLabelWidth_hos">
          <el-input v-model="form_hos.longitude" disabled></el-input>
        </el-form-item>

        <el-form-item label="维度" :label-width="formLabelWidth_hos">
          <el-input v-model="form_hos.latitude" disabled></el-input>
        </el-form-item>

        <el-form-item label="选择经纬度" :label-width="formLabelWidth_hos">
          <el-button @click="show_map">打开地图</el-button>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialog_hos = false">取 消</el-button>
        <el-button type="primary" @click="save_hos">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="选择地点" :visible.sync="dialog_map" width="70%">
      <div id="myPageTop">
        <table>
          <tr>
            <td>
              <label>请输入关键字：</label>
            </td>
          </tr>
          <tr>
            <td>
              <input id="tipinput" />
            </td>
          </tr>
        </table>
      </div>

      <div id="Map" style="height: 600px"></div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialog_map = false">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapActions, mapState } from "vuex";

import pca from "../../assets/pca.json";
export default {
  computed: {
    // ...mapState('状态机名'，['方法名1','方法名2'等])
    ...mapState("hospital", ["hos"]),
  },
  data() {
    return {
      test: "南昌县人民医院",
      pca,
      obj: {
        page: 1,
        pageSize: 10,
      },
      title: "新增医院",
      dialog_hos: false,
      dialog_map: false,
      form_hos: {},
      formLabelWidth_hos: "120px",
      map: {},
      result: 0,
    };
  },
  watch: {
    obj: {
      handler: function () {
        this.findHos(this.obj);
      },
      deep: true,
    },
  },
  created() {
    this.findHos(this.obj);
    this.page_query_hos();
    // this.judge(this.form_hos);
  },
  methods: {
    ...mapActions("hospital", ["findHos", "saveHos", "deleteHos"]),
    // 显示模态框
    show_dialog() {
      this.dialog_hos = true;
      this.title = "新增医院";
      this.form_hos = {};
    },
    initMap() {
      this.map = new AMap.Map("Map", {
        resizeEnable: true,
      });
      // console.log(AMap)
      var autoOptions = {
        input: "tipinput",
      };
      var auto = new AMap.Autocomplete(autoOptions);
      this.placeSearch = new AMap.PlaceSearch({
        map: this.map,
      }); //构造地点查询类
      AMap.event.addListener(auto, "select", this.select); //注册监听，当选中某条记录时会触发
    },
    select(e) {
      console.log(e.poi);
      this.placeSearch.setCity(e.poi.adcode);
      this.placeSearch.search(e.poi.name); //关键字查询查询
    },
    page_query_hos() {
      this.findHos(this.obj);
    },
    // 获取经纬度
    showInfoClick(e) {
      this.form_hos.longitude = e.lnglat.getLng();
      this.form_hos.latitude = e.lnglat.getLat();
      //   console.log(e.lnglat.getLng(), e.lnglat.getLat());
    },
    // 给地图绑定单击事件
    clickOn() {
      log.success("请点击选择地址！");
      this.map.on("click", this.showInfoClick);
    },
    //删除
    delete_hos(id) {
      // console.log(id);
      this.deleteHos({ id }).then((res) => {
        this.findHos(this.obj);
        this.dialog_hos = false;
        if (res.status == 200) {
          this.$notify({
            title: "成功",
            message: res.message,
            type: "success",
          });
        } else if (res.status == 500) {
          this.$notify({
            title: "错误",
            message: res.message + "输入数据有误！",
            type: "error",
          });
        }
      });
    },
    //修改
    edit_hos(val) {
      this.result == 0;
      this.title = "修改医院";
      // 1. 弹出模态框
      this.dialog_hos = true;
      // 2. 将当前行数据放入模态框
      this.form_hos = JSON.parse(JSON.stringify(val));
    },
    change(val) {
      // console.log(val);
      this.obj.page = val;
    },
    // 判断重复
    judge(arr) {
      this.findHos({ page: 1, pageSize: 1000 }).then((res) => {
        console.log(this.hos.list.length);
        for (let index = 0; index < this.hos.list.length; index++) {
          console.log(this.hos.list[index].name);
          if (arr == this.hos.list[index].name) {
            this.result = 1;
          }
        }
      });
      // return this.result;
    },
    // 保存医院
    save_hos() {
      if (this.form_hos.pca) {
        this.form_hos.province = this.form_hos.pca[0];
        this.form_hos.city = this.form_hos.pca[1];
        this.form_hos.area = this.form_hos.pca[2];
        // delete this.form_hos.pca;
      }
      this.judge(this.form_hos.name);
      setTimeout(() => {
        if (this.result == 0) {
          this.saveHos(this.form_hos).then((res2) => {
            this.findHos(this.obj);
            this.dialog_hos = false;
            if (res2.status == 200) {
              this.$notify({
                title: "成功",
                message: res2.message,
                type: "success",
              });
            } else if (res2.status == 500) {
              this.$notify({
                title: "错误",
                message: res2.message + "输入数据有误！",
                type: "error",
              });
            }
            this.form_hos = {};
          });
        } else if (this.title == "修改医院") {
          this.result = 0;
          this.saveHos(this.form_hos).then((res2) => {
            this.findHos(this.obj);
            this.dialog_hos = false;
            if (res2.status == 200) {
              this.$notify({
                title: "成功",
                message: res2.message,
                type: "success",
              });
            } else if (res2.status == 500) {
              this.$notify({
                title: "错误",
                message: res2.message + "输入数据有误！",
                type: "error",
              });
            }
            this.form_hos = {};
          });
        } else {
          this.result = 0;
          this.$notify({
            title: "错误",
            message: "录入医院已存在！",
            type: "error",
          });
        }
      }, 200);

      // console.log(this.form_hos);
    },
    show_map() {
      this.dialog_map = true;
      setTimeout(() => {
        this.initMap();
        this.clickOn();
      }, 500);
    },
    handleChange() {},
  },
};
</script>